package com.lv.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lv.mapper.*;
import com.lv.model.*;
import com.lv.service.ItemService;
import com.lv.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {

    @Resource
    private TbItemCatMapper im;
    @Resource
    private TbTypeTemplateMapper tm;
    @Resource
    private TbSpecificationOptionMapper som;
    @Resource
    private TbSpecificationMapper sm;
    @Resource
    private TbItemMapper itemMapper;
    //查询

    @Override
    public List<ItemVo> getInfo(Long parentId) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbItemCat> item = im.selectByExample(example);
        List<ItemVo> vo = JSONObject.parseArray(JSON.toJSONString(item), ItemVo.class);
        return vo;
    }

    //查询单条

    @Override
    public ItemVo select(Long id) {
        TbItemCat tbItemCat = im.selectByPrimaryKey(id);
        ItemVo vo = new ItemVo();
        BeanUtils.copyProperties(tbItemCat,vo);
        return vo;
    }

    //增加

    @Override
    public void addItem(ItemVo vo) {
        TbItemCat item = new TbItemCat();
        BeanUtils.copyProperties(vo,item);
        im.insertSelective(item);
    }

    //根据Id查询父级的Id

    @Override
    public ItemVo to(Long id) {
        ItemVo vo = new ItemVo();
        TbItemCat t1 = im.selectByPrimaryKey(id);
        Long a = t1.getParentId();
        if (a == 0) {
            return vo;
        }
        TbItemCat item = im.selectByPrimaryKey(a);
        BeanUtils.copyProperties(item,vo);
        return vo;
    }

    //修改

    @Override
    public void update(ItemVo vo) {
        TbItemCat item = new TbItemCat();
        BeanUtils.copyProperties(vo,item);
        im.updateByPrimaryKeySelective(item);
    }

    //删除

    @Override
    public void delete(List<Long> ids) {
        //先删除他的子类
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdIn(ids);
        im.deleteByExample(example);
        //删除本身
        TbItemCatExample example1 = new TbItemCatExample();
        TbItemCatExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andIdIn(ids);
        im.deleteByExample(example1);

    }

    @Override
    public TypeVo getType(Long itemCatId) {
        TbItemCat item = im.selectByPrimaryKey(itemCatId);
        TbTypeTemplate type = tm.selectByPrimaryKey(item.getTypeId());
        TypeVo vo = new TypeVo();
        BeanUtils.copyProperties(type,vo);
        //从模板中取出规格的字符串
        String specIds = vo.getSpecIds();
        //然后转换成集合
        List<SpecVo> specVos = JSONObject.parseArray(specIds, SpecVo.class);
        //根据specVos的 Id 查看 option表
        for (SpecVo specVo : specVos) {
            Long id = specVo.getId();
            TbSpecificationOptionExample example = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
            criteria.andSpecIdEqualTo(id);
            List<TbSpecificationOption> tbSpecificationOptions = som.selectByExample(example);
            //查出spec的名字
            TbSpecification tbSpecification = sm.selectByPrimaryKey(id);
            specVo.setSpecName(tbSpecification.getSpecName());
            specVo.setSpecOptionsList(tbSpecificationOptions);
        }
        vo.setSpecVO2s(specVos);
        return vo;
    }

    //扣减库存

    @Override
    public void subStockCount(List<TemVo> itemVoList) {
        for (TemVo temVo : itemVoList) {
            //根据Id查询数据
            TbItem item = itemMapper.selectByPrimaryKey(temVo.getId());
            //减库存
            item.setStockCount(item.getStockCount() - temVo.getNum());
            //修改数据库
            itemMapper.updateByPrimaryKeySelective(item);
        }
    }
}
